#include "stdSqlliteDatebase.h"
#include<vector>
#include<iostream>

bool StdSqliteBase::connectDB(const char *localname)
{
    int ret=sqlite3_open(localname,&m_db);
    if(ret!=SQLITE_OK)
    {
       std::cout<<"error:"<<sqlite3_errmsg(m_db)<<std::endl;
       return false;
     }
    return true;
}

bool StdSqliteBase::execute(const std::string &sql)
{
    //执行sql语句
   int ret=sqlite3_exec(m_db,sql.c_str(),NULL,NULL,NULL);
   if(ret!=SQLITE_OK)
   {
       std::cout<<"error:"<<sqlite3_errmsg(m_db)<<std::endl;
       return false;
   }
   return true;
}


QueryResult StdSqliteBase::query(const std::string &sql)
{
    char **table=NULL;
    int row=0;
    int column=0;

    /*获取数据库的值*/
    sqlite3_get_table(m_db,sql.c_str(),&table,&row,&column,NULL);
    QueryResult result;
    result.datas.resize(row,std::vector<std::string>(column));

     for(int idx=0;idx<column;idx++)
    {
         result.coluemName.push_back(table[idx]);
    }
    std::cout<<std::endl;

   for(int i = 1; i <= row; i++) {
        std::vector<std::string> rowdata;
        for(int j = 0; j < column; j++)
        {
           rowdata.push_back(table[i*column+j]);
        }
        result.datas.push_back(rowdata);
     
   }
   return result;
   
}
void StdSqliteBase::close()
{
    sqlite3_close(m_db);
}
